package com.audi.box.controller;

import java.util.ArrayList;

import android.content.Context;
import com.audi.box.adapter.SQLiteAsusHelperScene;
import com.audi.box.service.SceneService;
import com.audi.box.service.SceneServiceImp;
import com.audi.dto.SceneDeviceItem;
import com.audi.dto.SceneItem;

/**
 * @author CuongPH7
 * 
 */
public class SceneController implements IController {
    private SceneService sceneService;
    
    /**
     * Class constructor specifying Context to create
     * 
     * @param context
     *            android.content.Context
     * @throws Exception
     *             if (can not create SceneServiceImp
     */
    public SceneController(Context context) throws Exception {
        sceneService = new SceneServiceImp(new SQLiteAsusHelperScene(context));
    }
    
    /**
     * Class constructor specifying SceneService to create
     * 
     * @param sceneService
     *            SceneService
     * @see SceneService
     */
    public SceneController(SceneService sceneService) {
        this.sceneService = sceneService;
    }
    
    /**
     * Inject SceneService
     * 
     * @param sceneService
     *            SceneService
     * @see SceneService
     */
    public void setSceneService(SceneService sceneService) {
        this.sceneService = sceneService;
    }
    
    /**
     * Add new scene item by Scene Information
     * 
     * @param sceneName
     *            Scene Name
     * @param sceneIcon
     *            Scene Icon
     * @param sceneColor
     *            Scene Color
     * @param scenePlace
     *            Scene's Place
     * @return Scene Id if successful, 0 if (scene is exist
     * @throws Exception
     *             if (scene is null or scene information missing
     */
    public int addScene(String sceneName, String sceneIcon, int sceneColor,
            int scenePlace) {
        SceneItem scene = new SceneItem();
        
        scene.setSceneTitle(sceneName);
        scene.setSceneIcon(sceneIcon);
        scene.setSceneBackground(sceneColor);
        scene.setPlaceId(scenePlace);
        return addScene(scene);
    }
    
    /**
     * Add new scene item by SceneItem
     * 
     * @param scene
     *            SceneItem
     * @return Scene Id if successful, 0 if (scene is exist
     * @throws Exception
     *             if (scene is null or scene information missing
     */
    public int addScene(SceneItem scene) {
        if (scene == null) {
            throw new NullPointerException("Scene is Null");
        }
        
        if (scene.getSceneTitle() == null || scene.getSceneTitle().equals("")
                || scene.getSceneIcon() == null
                || scene.getSceneIcon().equals("")) {
            throw new NullPointerException("Scene Information is missing");
        } else if ((scene.getSceneId() != null)
                && (scene.getSceneId() != 0)
                && isExist(scene)) {
            return 0;
        }
        
        return sceneService.addScene(scene);
    }
    
    /**
     * Update scene item
     * 
     * @param scene
     *            SceneItem
     * @return Scene Id if successful, 0 if scene is exist
     * @throws Exception
     *             if (scene is null or scene information missing
     */
    public int updateScene(SceneItem scene) {
        if (scene == null) {
            throw new NullPointerException("Scene is Null");
        }
        
        return sceneService.updateScene(scene);
    }
    
    /**
     * Delete a Scene
     * 
     * @param scene
     *            SceneItem
     * @return 1 if successful, 0 if scene is not exist
     */
    public int deleteScene(SceneItem scene) {
        if (scene == null) {
            throw new NullPointerException("Scene is Null");
        }
        
        return sceneService.deleteScene(scene);
    }
    
    /**
     * Get Scene Item information
     * 
     * @param sceneId
     *            Scene Id
     * @return SceneItem
     */
    public SceneItem getSceneById(int sceneId) {
        return sceneService.getScene(sceneId);
    }
    
    /**
     * Get All Scene List
     * 
     * @return An ArrayList SceneItem
     */
    public ArrayList<SceneItem> getSceneList() {
        return sceneService.getListScene();
    }
    
    /**
     * Get Scene List by place id
     * 
     * @return An ArrayList SceneItem
     */
    public ArrayList<SceneItem> getSceneListByPlaceId(int placeId) {
        return sceneService.getListScene(placeId);
    }
    
    /**
     * Check Scene is Exist
     * 
     * @return <code>true</code> if Scene is Exist <code>false</code> if Scene
     *         does not Exist
     */
    public boolean isExist(SceneItem scene) {
        if (scene == null) {
            throw new NullPointerException("Scene is Null");
        }
        return sceneService.isExist(scene);
    }
    
    /**
     * Add SceneDeviceItem
     * <p>
     * Added 12/09
     * 
     * @author CuongPH7
     * @param SceneDeviceItem
     * @return 1 if successful, 0 if (scene is not exist
     * @see SceneDeviceItem
     */
    public int addSceneDeviceItem(SceneDeviceItem sceneDevice) {
        if (sceneDevice == null) {
            throw new NullPointerException("Scene Device is Null");
        }
        else if ((sceneDevice.getScene() == null)
                || (sceneDevice.getDevice() == null)) {
            throw new NullPointerException("Scene Device is missing");
        }
        
        return sceneService.addSceneDevice(sceneDevice);
        
    }
    
    /**
     * Get Scene Device Item
     * <p>
     * Added 12/09
     * 
     * @author CuongPH7
     * @param sceneId
     * @param deviceId
     * @return SceneDeviceItem
     * @see SceneDeviceItem
     */
    public SceneDeviceItem getSceneDevice(int sceneId, int deviceId) {
        if ((sceneId == 0)
                || (deviceId == 0)) {
            throw new NullPointerException("Scene or Device id are missing");
        }
        
        return sceneService.getSceneDevice(sceneId, deviceId);
    }
    
    /**
     * Get Scene Device List
     * <p>
     * Added 12/09
     * 
     * @author CuongPH7
     * @return An ArrayList<SceneDeviceItem>
     * @see SceneDeviceItem
     */
    public ArrayList<SceneDeviceItem> getSceneDeviceList() {
        return sceneService.getListSceneDevice();
    }
    
    /**
     * Delete a SceneDeviceItem.
     * <p>
     * Added 12/09
     * 
     * @author CuongPH7
     * @return 1 if successful, 0 if scene is exist
     * @see SceneDeviceItem
     */
    public int deleteSceneDeviceItem(SceneDeviceItem sceneDevice) {
        return sceneService.deleteSceneDeviceItem(sceneDevice);
    }
}
